const log = require("../utils/log");
const Redis = require('./Redis');
const game_config = require('../config/game_config');

class RedisBase extends Redis {
    static getInstance(){
        if(!RedisBase.instance){
            RedisBase.instance = new RedisBase();
            return RedisBase.instance;
        }else{
            return RedisBase.instance;
        }
    }

    constructor() {
        super(game_config.center_redis.host,game_config.center_redis.port,game_config.center_redis.db_index);
        console.log('==========Redis　RedisBase===========');

        // this.connectRedis(game_config.center_redis.host,game_config.center_redis.port,game_config.center_redis.db_index);
    }

    async closeRedis(){
        await this.quit()
    }

    //----------------------------------------hs_userInfo_uid＿　start-------------------------------------------------
    async getUserList(){
        let key = `list_userInfo_uid`;
        let list = await this.lrange(key,0,-1);
        return list;
    }

    //根据uid获取用户数据
    async getUserInfo(uid){
        // log.info('========getUserInfoByUid=========',uid);
        let key = `list_userInfo_uid`;
        let res = null;
        let list = await this.getUserList();
        for (let i = 0; i < list.length; i++) {
            let cfg = JSON.parse(list[i]);
            if(cfg.userId === uid){
                res = JSON.parse(list[i]);
                break;
            }
        }
        return res;
    }
    //将数据存到redis
    async setUserInRedis(user){
        // log.info('========setUserInRedis=========',user);
        let rdKey = `list_userInfo_uid`;
        let res = await this.lpush(rdKey,user);
        return res;
    }

    //更新列表数据
    async updateUserist(index,value){
        let key = 'list_userInfo_uid';
        let res = await this.lset(key, index, value)
        return res;
    }

    async setHashField(uid, field, value) {
        log.info('======setHashField============',uid,field,value)
        let rdKey = `list_userInfo_uid`;
        let list =  await this.getUserList();
        for (let i = 0; i < list.length; i++) {
            let cfg = JSON.parse(list[i]);
            if(cfg.userId === uid){
                if(field === "ballNum"){
                    cfg.ballNum = cfg.ballNum*1 + value;
                }else if(field === "ticket"){
                    cfg.ticket = cfg.ticket*1 +value;
                }else if(field === 'isNewPlayer'){
                    cfg.isNewPlayer = value;
                }
                await this.updateUserist(i,JSON.stringify(cfg));
                break;
            }
        }
    }

    //更新球币
    async updateUserQiuNum(uid,num){
        await this.setHashField(uid,'ballNum',num);
    }
    //更新消费券数量
    async updateUserQuanNum(uid,num){
        await this.setHashField(uid,'ticket',num);
    }
    //更新是新用户属性
    async updateUserisNewPlayer(uid,num){
        await this.setHashField(uid,'isNewPlayer',num);
    }

    //----------------------------------------hs_userInfo_uid＿　end-------------------------------------------------

    //========================================room_list_zts start=======================================
    //添加主题赛房间列表
    async addRoomInfoToList(roomInfo){
        let key = 'room_list_zts';
        let res = await this.lpush(key,roomInfo);
        return res;
    }

    //获取有效的房间
    async getValidRoomInList(){
        let key = 'room_list_zts';
        let res = await this.lrange(key, 0, -1)
        return res;
    }

    //从列表中删除一条数据
    async delRooomList(index,value){
        let key = 'room_list_zts';
        let res = this.lrem(key, index, value);
        return res;
    }
    //跟新数据
    async updateRoomListValue(index,value){
        let key = 'room_list_zts';
        let res = await this.lset(key, index, value)
        return res;
    }
    //========================================room_list_zts end=======================================

    //========================================gameInfo_list_zt start=======================================
    //主题赛游戏数据列表
    async addGameZtInfoInfoToList(gameInfo){
        let key = 'gameInfo_list_zt';
        let res = await this.lpush(key,gameInfo);
        return res;
    }

    //获取有效的房间 type是类型
    // type = 1, 文体
    // type = 2, 足球
    // type = 3, 名族
    async getGameListZt(){
        let key = 'gameInfo_list_zt';
        let res = await this.lrange(key, 0, -1);
        return res;
    }

    async getGameInfoZtByUid(uid){
        let key = 'gameInfo_list_zt';
        let res = null;
        let list = await this.getGameListZt();
        for (let i = 0; i < list.length; i++) {
            let cfg = JSON.parse(list[i]);
            if(cfg.uid === uid){
                res = cfg;
                break;
            }
        }
        return res;
    }

    //更新gameInfo list 数据
    async updateGameListTzValue(index,value){
        let key = 'gameInfo_list_zt';
        let res = await this.lset(key, index, value);
        return res;
    }

    //========================================gameInfo_list_zt end=======================================

    //=====================================game_tz_cfg start========================================
    //每日挑战配置
    async addGameTzCfgToList(cfg){
        let key = 'game_tz_cfg_list';
        let res = await this.lpush(key,cfg);
        return res;
    }

    async getGameTzCfgList(){
        let key = 'game_tz_cfg_list';
        let res = await this.lrange(key, 0, -1);
        return res;
    }
    async getGameTzCfgByType(type){
        let list = await this.getGameTzCfgList();
        let res = null;
        for (let i = 0; i < list.length; i++) {
            let cf = JSON.parse(list[i]);
            if(cf.gtype === type){
                res = cf;
                break;
            }
        }
        return res;
    }

    //保存用户游戏数据到列表
    async addGameTzInfoToList(cfg){
        let key = 'game_tz_info_list';
        let res = await this.lpush(key,cfg);
        return res;
    }

    //取用户游戏数据列表
    async getGameTzInfoList(){
        let key = 'game_tz_info_list';
        let res = await this.lrange(key, 0, -1);
        return res;
    }

    async getGametzInfoByUid(uid){
        let list = await this.getGameTzInfoList();
        let res = null;
        for (let i = 0; i < list.length; i++) {
            let cfg = JSON.parse(list[i]);
            if(cfg.uid === uid){
                res = cfg;
                break;
            }
        }
        return res;
    }

    //修改值
    async updateGameTzInfoList(idx, value){
        let key = 'game_tz_info_list';
        let res = await this.lset(key, idx, value);
        return res;
    }

    //=====================================game_tz_cfg end========================================

    //存放需要定时重置的数据
    //=====================================game_reset_data start========================================
    //添加重置数据
    async addResetToList(cfg){
        let key = 'game_reset_data';
        let res = await this.lpush(key,cfg);
        return res;
    }

    //取重置数据列表
    async getResetList(){
        let key = 'game_reset_data';
        let res = await this.lrange(key, 0, -1);
        return res;
    }

    //更新重置数据列表
    async updateResetList(idx,value){
        let key = 'game_reset_data';
        let res = await this.lset(key, idx, value);
        return res;
    }

    //根据uid取指定的重置数据
    async getResetInfoByUid(uid){
        let res = null;
        let key = 'game_reset_data';
        let list = await this.getResetList();
        for (let i = 0; i < list.length; i++) {
            let cfg = JSON.parse(list[i]);
            if(cfg.uid === uid){
                res = cfg;
                break;
            }
        }
        return res;
    }
    //=====================================game_reset_data end========================================
}
module.exports = RedisBase.getInstance();